//
//  MenuItemView.m
//  JiaoX
//
//  Created by yinqy on 15/3/20.
//  Copyright (c) 2015年 北京娇羞科技有限公司. All rights reserved.
//

#import "MenuItemView.h"

#define RED_POINT_WIDTH 16
@interface MenuItemView()
{
    UIImageView *_iconImg;
    UILabel *_titleLabel;
    UILabel *_badgeLabel;
    
    PopMenuBean *_bean;
}

@end
@implementation MenuItemView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setSize:CGSizeMake(MTV_WIDTH, MTV_HEIGHT)];
        [self setBackgroundImage:[UIImage imageWithColor:JXColor(204, 204, 204)] forState:UIControlStateHighlighted];
        
        _iconImg = [[UIImageView alloc]initWithFrame:CGRectMake(10, (MTV_HEIGHT-20)/2, 20, 20)];
        [self addSubview:_iconImg];
        
        _titleLabel = [[UILabel alloc]init];
        [_titleLabel setSize:CGSizeMake(0, [UIFont systemFontOfSize:16].lineHeight)];
        [_titleLabel setX:CGRectGetMaxX(_iconImg.frame)+5];
        [_titleLabel setY:(MTV_HEIGHT-_titleLabel.height)/2];
        [_titleLabel setLineBreakMode:NSLineBreakByTruncatingMiddle];
        [_titleLabel setFont:[UIFont systemFontOfSize:16]];
        [self addSubview:_titleLabel];
        
        _badgeLabel = [[UILabel alloc]init];
        [_badgeLabel setBackgroundColor:[UIColor redColor]];
        [_badgeLabel setTextColor:[UIColor whiteColor]];
        [_badgeLabel setTextAlignment:NSTextAlignmentCenter];
        [_badgeLabel setFont:[UIFont systemFontOfSize:10]];
        [_badgeLabel setSize:CGSizeMake(RED_POINT_WIDTH, RED_POINT_WIDTH)];
        [_badgeLabel.layer setCornerRadius:RED_POINT_WIDTH/2];
        [_badgeLabel.layer setMasksToBounds:YES];
        [self addSubview:_badgeLabel];
        
    }
    return self;
}

-(instancetype)initWithPopMenuBean:(PopMenuBean *)bean{
    self = [super init];
    if (self) {
        [self setDataWithPopMenuBean:bean];
    }
    return self;
}

-(void)setDataWithPopMenuBean:(PopMenuBean *)bean{
    _bean = bean;
    [_titleLabel setText:bean.title];
    
    if ([Utils isBlankString:bean.icon]) {
        _iconImg.hidden = YES;
        [_titleLabel setTextAlignment:NSTextAlignmentCenter];
        [_titleLabel setWidth:MTV_WIDTH];
        [_titleLabel setX:0];
    }else{
        _iconImg.hidden = NO;
        if ([bean.icon hasPrefix:@"http"]) {
            [_iconImg.layer setCornerRadius:_iconImg.width/2];
            [_iconImg.layer setMasksToBounds:YES];
            [_iconImg sd_setImageWithURL:[NSURL URLWithString:bean.icon]];
        }else{
            if ([bean.icon isEqualToString:@"ic_launcher"]) {
                [_iconImg setContentMode:UIViewContentModeScaleAspectFit];
            }else{
                [_iconImg setContentMode:UIViewContentModeCenter];
            }
            
            [_iconImg setImage:[UIImage imageNamed:bean.icon]];
        }
        [_titleLabel setSize:[_titleLabel.text sizeWithFont:_titleLabel.font constrainedToSize:[@"娇羞程序员**" sizeWithFont:_titleLabel.font]]];
    }
    if (bean.bageNum==0) {
        [_badgeLabel setHidden:YES];
    }else{
        [_badgeLabel setText:[NSString stringWithFormat:@"%ld",(long)bean.bageNum]];
        [_badgeLabel setHidden:NO];
        //调整位置
        [_badgeLabel setX:CGRectGetMaxX(_titleLabel.frame)+5];
        [_badgeLabel setY:CGRectGetMinY(_titleLabel.frame)-5];
    }
}

-(void)updateItem{
    [self setDataWithPopMenuBean:_bean];
}


@end
